package com.huawang.mdesign.model.service.inter;

import com.huawang.business.command.CommandAddType;
import com.huawang.business.constants.ErrorCode;
import com.huawang.business.dto.AddModelDTO;
import com.huawang.business.dto.ModifyPropertyDTO;
import com.huawang.business.exception.AbortExecutionException;
import com.huawang.business.service.model.core.property.PropertyManager;
import com.huawang.business.service.transaction.TransactionServiceImpl;
import com.huawang.business.util.InstanceVoUtils;
import com.huawang.business.vo.ModelResultVo;
import com.huawang.core.definition.mof.UMLDefinitionConstants;
import com.huawang.core.definition.mof.instances.MetaClassInstance;
import com.huawang.core.semantics.entity.transaction.LocalTransaction;
import com.huawang.core.semantics.exception.LocalTransactionNotStartException;
import com.huawang.core.semantics.exception.TransactionNotExistingException;
import com.huawang.mdesign.model.service.dto.AbsentTriggerModifyPropertyDTO;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import static com.huawang.business.constants.ModelActionName.ADD_MODEL;

@Service
@Primary
public class RelatedPropertyService {
    @Autowired
    private PropertyManager propertyManager;

    @Autowired
    private TransactionServiceImpl transactionService;

    @Autowired
    private ModelService modelService;

    public ModelResultVo updateValueWithNoTrigger(@NotNull final AbsentTriggerModifyPropertyDTO dto) {
        final String projectId = dto.getProjectId();
        AddModelDTO addModelDTO = new AddModelDTO();
        addModelDTO.setProjectId(projectId);
        addModelDTO.setMetaclass(UMLDefinitionConstants.Trigger);
        addModelDTO.setType(CommandAddType.MODEL.getCode());
        addModelDTO.setParentId(dto.getTransitionId());
        ModelResultVo modelResultVo = new ModelResultVo();
        LocalTransaction transaction = transactionService.createTransaction(projectId, ADD_MODEL);
        try {
            //创建触发器
            MetaClassInstance result = modelService.addModel(addModelDTO, null, false);
            ModifyPropertyDTO modifyPropertyDTO = new ModifyPropertyDTO();
            modifyPropertyDTO.setInstanceId(result.getId());
            modifyPropertyDTO.setProjectId(projectId);
            modifyPropertyDTO.setKey(dto.getKey());
            modifyPropertyDTO.setValue(dto.getValue());
            modifyPropertyDTO.setUpdateMethod(dto.getUpdateMethod());
            modifyPropertyDTO.setDataType(dto.getDataType());
            modifyPropertyDTO.setIsMultiplicity(dto.getIsMultiplicity());
            //更新eventType属性
            propertyManager.updatePropertyValue(modifyPropertyDTO);
            transactionService.commit(projectId);
            modelResultVo.setInstanceId(result.getId());
            InstanceVoUtils.fillAddModelResultVo(modelResultVo, transaction);
        } catch (TransactionNotExistingException | LocalTransactionNotStartException e) {
            throw AbortExecutionException.build(ErrorCode.TRANSACTION_ERROR, e);
        } catch (Exception e) {
            transactionService.rollBack(projectId, transaction);
            throw AbortExecutionException.build(ErrorCode.PROPERTY_UPDATE_ERROR, e);
        }

        return modelResultVo;
    }
}
